package com.ckzp.jfinal.weixin.controller;

import com.ckzp.core.model.Datas;

import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.system.cache.SysDataCache;
import com.ckzp.jfinal.weixin.model.WxMember;
import com.ckzp.jfinal.weixin.validate.WxMemberValidator;
import com.ckzp.plugins.layui.LayuiData;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by 51594 on 2021/6/4.
 */
public class WxMemberController extends BaseController {

    @Admins(roles = "system", name = "默认", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 列表
     */
    @Admins(roles = "system", name = "列表PAGE", log = false)
    public void WxMemberList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("WxMemberList.html");
    }

    /**
     * 微信用户数据查询API
     */
    @Admins(roles = "system", name = "微信用户数据查询API", log = false)
    public void WxMemberListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "wx_member");
        ps.orderby(getRequest(), "last_time desc");
        Page<WxMember> pages = ps.paginate(WxMember.dao);
        List<WxMember> datalist = pages.getList();
        for (WxMember user : datalist) {
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }


    /**
     * 修改
     */
    @Admins(roles = "system", name = "修改", log = false)
    public void EditWxMember() {
        int mem_id = getParaInt("mem_id", 0);
        WxMember o = WxMember.dao.findById(mem_id);
        setAttr("member", o);
        render("ModifyWxMember.html");
    }

    /**
     * 修改企业员工Api
     */
    @Before(WxMemberValidator.class)
    @Admins(roles = "system", name = "修改企业员工Api", log = true)
    public void ModifyWxMemberApi() {
        Datas data = getDatas();
        int mem_id = data.getInt("mem_id");
        boolean results = WxMember.dao.findById(mem_id)
                .set("user_id", data.getInt("user_id", 0))
                .set("user_nick", data.getStr("user_nick", ""))
                .set("user_desc", data.getStr("user_desc", ""))
                .update();
        renderJson(Json(results, "修改"));
    }

    /**
     * 改变房间字段状态Api(0 or 1)
     */
    @Admins(roles = "system", name = "改变状态Api", log = true)
    public void ChangeWxMemberTagApi() {
        String keyid = getParaStr("key", "");
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(WxMember.dao, keyid, field, msg));
    }
}

